<template>
    <el-row :gutter="gutter" class="upload-container"> <!--16:9添加cover-map-->
        <el-col :span="span">
            <el-upload
                :action="uploadMediaUrl"
                :accept="media"
                :show-file-list="false"
                :on-success="uploadSuccess"
                :on-error="uploadError"
                :before-upload="uploadBefore"
                :disabled="disabled"
            >
                <div class="layout-block" :style="{'padding-bottom': pb, 'border-radius': radius}">
                    <div v-loading="uploading" class="layout-item upload-btn" :class="{'has-img':uploadedImgs}" :style="{'border-radius': radius}">
                        <i v-show="!disabled" :class="uploadedImgs?'el-icon-edit-outline':'el-icon-plus'" />
                        <span v-if="size" class="upload-size">{{ $t('global.recomSize') }}：{{ size }}</span>
                        <img v-if="fileType === 'image'" v-show="uploadedImgs" :src="uploadedImgs" alt="" class="uploaded-single" :style="{'object-fit': fit}">
                        <video v-if="fileType === 'video'" v-show="uploadedImgs" :src="uploadedImgs" controls class="uploaded-single" :style="{'object-fit': fit}" />
                    </div>
                </div>
            </el-upload>
        </el-col>
    </el-row>
</template>

<script>
import { uploadMediaUrl } from '@/api/admin/upload'
import { checkFileType } from '@/utils'

/**
 * @desc 上传媒体
 * media - 上传类型：image video
 * size - 建议尺寸
 * pb - 图片比例
 * fit - 填充：cover contain
 * radius - 圆角
 */
export default {
    name: 'UploadMediaBoth',
    props: {
        gutter: {
            type: Number,
            default: 20 // 间隔 px
        },
        span: {
            type: Number,
            default: 24 // 分栏 1/24
        },
        value: {
            type: String,
            default: ''
        },
        media: {
            type: String,
            default: 'image/*, video/*' // 同时
        },
        disabled: {
            type: Boolean,
            default: false
        },
        size: {
            type: String,
            default: '' // 1920*1080
        },
        pb: {
            type: String,
            default: '100%'
        },
        fit: {
            type: String,
            default: 'contain'
        },
        radius: {
            type: String,
            default: '0px'
        }
    },
    data: function() {
        return {
            uploadMediaUrl,
            uploadedImgs: '',
            dialogVisible: false,
            dialogImageUrl: '',
            uploading: false
        }
    },
    computed: {
        fileType() {
            const fileName = this.uploadedImgs.split('.').pop() || ''
            return checkFileType(fileName)
        }
    },
    watch: {
        value() {
            this.uploadedImgs = this.value;
        },
        uploadedImgs(val) {
            this.$emit('input', this.uploadedImgs);
        }
    },
    created() {
        this.uploadedImgs = this.value;
    },
    methods: {
        uploadBefore(event, file, fileList) {
            if (this.uploading) return false;
            this.uploading = true;
        },
        uploadError(err) {
            console.log(err);
            this.uploading = false;
        },
        uploadSuccess(res) {
            this.uploading = false;
            this.uploadedImgs = res.data;
        },
        imgView(url) {
            window.open(url);
        },
        imgDelete(index) {
            this.uploadedImgs = '';
        }
    }
}
</script>

<style lang="scss" scoped>
.upload-container {
    .layout-block {
        position: relative;
        width: 100%;
        height: 0;
        padding-bottom: 100%;
        overflow: hidden;
    }

    .layout-item {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 4px;
        background-color: #999;
        overflow: hidden;

        .uploaded-img {
            display: block;
            width: 100%;
            height: 100%;
            object-fit: contain;
        }
    }

    .upload-btn {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border: 1px dashed #d9d9d9;
        background-color: #fbfdff;
        border-radius: 4px;
        transition: .3s;

        i {
            position: relative;
            font-size: 28px;
            color: #8c939d;
            transition: .3s;
            z-index: 5;
        }

        .upload-size {
            position: relative;
            font-size: 12px;
            line-height: 1;
            color: #999;
            z-index: 5;
        }

        .uploaded-single{
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            object-fit: contain;
            z-index: 1;
            transition: .3s;
        }
        &:hover {
            background-color: #e6ebf1;
            border-color: #1d38FF;
            i{
                color: #1d38FF;
            }
        }
        &.has-img {
            i {
                color: #fff;
                opacity: 0;
                filter: drop-shadow(0px 0px 4px black);
                transition: .3s;
            }

            .upload-size {
                color: #fff;
                opacity: 0;
                text-shadow: 0 0 4px black;
                transition: .3s;
            }

            &:hover {
                i {
                    opacity: 1;
                }
                .upload-size {
                    opacity: 1;
                }
            }
        }
    }

    ::v-deep {
        .el-upload {
            display: block;
        }
    }
}
.cover-map{
    .layout-block {
        padding-bottom: 56.25% !important;
    }
    .uploaded-single{
        object-fit: cover !important;
    }
}
</style>
